package day1109.aaa;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Mr.ma
 * @since 2022/11/9 14:50
 **/
public class HahaProxy implements MethodInterceptor {
    private Object target;

    public HahaProxy(Object target) {
        this.target = target;
    }

    public Object getTarget(){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        check(objects);
        Object invoke = method.invoke(target, objects);
        log(invoke);
        return invoke;
    }

    public static void check(Object[] objects){
        int count = objects.length;
        System.out.println("对参数个数进行校验，本次参与运算的参数有" + count + "个");
    }
    public static void log(Object result){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss SSS");
        System.out.println(dtf.format(now) + "  INFO======参数计算成功，结果为：" + result);
    }
}
